﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Collections.ObjectModel;
using System.IO;

namespace TfsExtensions.Build.Activities
{
    public sealed class CreateCommentAndAssemblyXmlList : CodeActivity
    {
        [RequiredArgument]
        public OutArgument<Collection<string>> CommentXmlList { get; set; }

        [RequiredArgument]
        public OutArgument<Collection<string>> CommentAssemblyList { get; set; }

        [RequiredArgument]
        public InArgument<string> PathToBinariesToProcess { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            Collection<string> xmlList = new Collection<string>();
            Collection<string> assemblyList = new Collection<string>();
            try
            {
                var assebmlyPath = this.PathToBinariesToProcess.Get(context);
                DirectoryInfo di = new DirectoryInfo(assebmlyPath);
                FileInfo[] dllFiles = di.GetFiles("*.dll");

                foreach (FileInfo fi in dllFiles)
                {
                    var fileName = fi.FullName.Substring(0, fi.FullName.LastIndexOf("."));
                    if (File.Exists(string.Format("{0}.xml", fileName)))
                    {
                        // Adding the xml file path for all dll in the directory
                        xmlList.Add(string.Format("{0}.xml", fileName));

                        // Add all the dlls who are having their relative xml files
                        assemblyList.Add(fi.FullName);
                    }

                    File.Copy(fi.FullName, string.Format("{0}.tmp", fi.FullName), true);
                }


                FileInfo[] exeFiles = di.GetFiles("*.exe");


                foreach (FileInfo fi in exeFiles)
                {
                    var fileName = fi.FullName.Substring(0, fi.FullName.LastIndexOf("."));
                    if (File.Exists(string.Format("{0}.xml", fileName)))
                    {
                        // Adding the xml file path for all exe in the directory
                        xmlList.Add(string.Format("{0}.xml", fileName));

                        // Add all the exe who are having their relative xml files
                        assemblyList.Add(fi.FullName);
                    }

                    File.Copy(fi.FullName, string.Format("{0}.tmp", fi.FullName), true);
                }

                CommentXmlList.Set(context, xmlList);
                CommentAssemblyList.Set(context, assemblyList);

                if (xmlList.Count == 0 && assemblyList.Count == 0)
                {
                    LogHelper.LogWarning(context, "No Project selected for documentation");
                }
            }
            catch(Exception ex)
            {
                LogHelper.LogError(context,ex.Message);
                throw;
            }
        }
    }
}
